<script setup>
import ChannelEdit from './components/ChannelEdit.vue';
import {ref,watch,onMounted} from 'vue';
import { ElMessage } from 'element-plus';
import { Edit,Delete } from '@element-plus/icons-vue';
import PageContainer from '@/components/PageContainer.vue';

const dialog=ref(null);
const dialogVisibletwo=ref(false);
const currentChannelRow=ref(null);
const channelList=ref([
        {name:'微小说',alias:'Micro-fiction'},
        {name:'短篇小说',alias:'Short Story'},
        {name:'中篇小说',alias:'Novella'},
        {name:'长篇小说',alias:'Novel'},
        {name:'其他',alias:'other'},    
    ]);

const onAddChannel=()=>{
    dialog.value.open({})
}
const onEditChannel=(row)=>{
    dialog.value.open({row})
}
const onDelChannel=(row)=>{
    currentChannelRow.value=row
    dialogVisibletwo.value=true
}
const confirmDelete=()=>{
    if(currentChannelRow.value){
        channelList.value=channelList.value.filter(item=>item.name!==currentChannelRow.value.name || item.alias!==currentChannelRow.value.alias)    
    }
    dialogVisibletwo.value=false
    currentChannelRow.value=null
    ElMessage.success('删除成功！')
    window.dispatchEvent(new Event('channelListUpdated'));
}

const handleForm=({formData,isEdit,originalRow})=>{
    if(isEdit){
        const index=channelList.value.findIndex(item=>item.name===originalRow.name && item.alias===originalRow.alias)
        if(index!==-1){
            channelList.value.splice(index,1,formData)
            ElMessage.success('修改成功！')
        }
    }else{
        channelList.value.push(formData)
        ElMessage.success('添加成功！')
    }
}

watch(channelList,(newList)=>{
    localStorage.setItem('brotherChannelList',JSON.stringify(newList))
    window.dispatchEvent(new Event('channelListUpdated'));
},{deep:true})

onMounted(()=>{
    localStorage.setItem('brotherChannelList',JSON.stringify(channelList.value))
})
</script>

<template>
    <page-container title="文章分类">
        <template #extra>
            <el-button type="primary" @click="onAddChannel">添加分类</el-button>
        </template>
        <el-table v-loading="false" :data="channelList" style="width: 100%">
            <el-table-column label="序号" type="index" width="100"></el-table-column>
            <el-table-column prop="name" label="分类名称"></el-table-column>
            <el-table-column prop="alias" label="分类别名"></el-table-column>
            <el-table-column label="操作" width="180">
                <template #default="{row,$index}">
                    <el-button type="primary" :icon="Edit" size="small" @click="onEditChannel(row)" circle plain></el-button>
                    <el-button type="danger" :icon="Delete" size="small" @click="onDelChannel(row)" circle plain></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <div class="empty-box">
                    <el-empty description="No data"></el-empty>
                </div>
            </template>
        </el-table>
    </page-container>

    
    <ChannelEdit :channels="channelList" ref="dialog" @handleForm="handleForm"/>
    
    <el-dialog v-model="dialogVisibletwo" title="删除" width="500"><span>确认删除？</span>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisibletwo=false">取 消</el-button>
                <el-button type="primary" @click="confirmDelete">确 定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<style scoped lang="scss">
/* 1. 页面容器基础样式：控制整体间距与背景 */
.page-container {
  padding: 24px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 120px); /* 适配布局高度，避免内容过短时底部留白 */
}

/* 2. 顶部操作区（添加按钮）样式：与标题栏协调 */
:deep(.page-header) {
  display: flex;
  align-items: center;
  justify-content: space-between; /* 确保添加按钮居右（若之前未处理） */
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid #ebeef5; /* 分隔线增强区域感 */

  /* 标题样式优化 */
  .page-header-title {
    font-size: 18px;
    font-weight: 500;
    color: #1d2129;
  }

  /* 添加按钮微调：与右侧保持间距 */
  .el-button--primary {
    padding: 8px 16px;
    font-size: 14px;
  }
}

/* 3. 表格核心样式：美化+交互增强 */
.el-table {
  width: 100%;
  background-color: #fff;
  border-radius: 8px; /* 圆角优化 */
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06); /* 轻微阴影提升层次感 */
  overflow: hidden; /* 避免圆角与边框冲突 */

  /* 表头样式：区分内容区 */
  :deep(.el-table__header-wrapper) {
    background-color: #fafbfc;

    th {
      font-size: 14px;
      font-weight: 500;
      color: #4e5969;
      border-bottom: 1px solid #ebeef5 !important;
      padding: 14px 0 !important; /* 统一表头内边距 */
    }
  }

  /* 表体样式：行高与间距优化 */
  :deep(.el-table__body-wrapper) {
    tr {
      height: 56px; /* 统一行高，避免内容参差不齐 */

      /* 行 hover 效果：柔和过渡 */
      &:hover > td {
        background-color: #f7faff !important;
        transition: background-color 0.2s ease;
      }
    }

    td {
      font-size: 13px;
      color: #1d2129;
      border-bottom: 1px solid #f2f3f5 !important;
      padding: 12px 0 !important;
    }
  }

  /* 序号列与操作列：固定宽度+居中 */
  :deep(.el-table-column--index) {
    text-align: center !important;
    width: 100px !important; /* 匹配模板中设置的宽度 */
  }

  :deep(.el-table-column:last-child) {
    text-align: center !important;
    width: 180px !important; /* 操作列固定宽度 */
  }
}

/* 4. 操作按钮样式：hover 增强+间距控制 */
:deep(.el-table-column__content .el-button) {
  margin: 0 6px; /* 按钮间距 */
  width: 32px;
  height: 32px;
  padding: 0; /* 圆形按钮取消内边距 */
  transition: all 0.2s ease;

  /* 编辑按钮：蓝色系 */
  &.el-button--primary.is-plain {
    color: #4096ff;
    border-color: #4096ff;

    &:hover {
      background-color: #4096ff;
      color: #fff;
    }
  }

  /* 删除按钮：红色系 */
  &.el-button--danger.is-plain {
    color: #f56c6c;
    border-color: #f56c6c;

    &:hover {
      background-color: #f56c6c;
      color: #fff;
    }
  }
}

/* 5. 空状态样式：优化空白时的视觉体验 */
.empty-box {
  padding: 60px 0; /* 增加空状态区域高度，避免过于紧凑 */
  text-align: center;

  :deep(.el-empty) {
    .el-empty__image {
      width: 120px;
      height: 120px;
      margin-bottom: 16px;
    }

    .el-empty__description {
      font-size: 14px;
      color: #86909c;
    }
  }
}

/* 6. 删除弹窗样式：美化+内容优化 */
:deep(.el-dialog) {
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);

  /* 弹窗标题 */
  .el-dialog__title {
    font-size: 16px;
    font-weight: 500;
    color: #1d2129;
  }

  /* 弹窗内容 */
  .el-dialog__body {
    font-size: 14px;
    color: #4e5969;
    padding: 24px;
    line-height: 1.5;
  }

  /* 弹窗底部按钮 */
  .el-dialog__footer {
    padding: 16px 24px;
    border-top: 1px solid #ebeef5;

    .el-button {
      padding: 8px 16px;
      font-size: 14px;

      &.el-button--primary {
        background-color: #4096ff;
        border-color: #4096ff;
      }
    }
  }
}

/* 7. 响应式适配：小屏幕优化 */
@media (max-width: 768px) {
  .page-container {
    padding: 16px;
  }

  /* 表格：小屏幕横向滚动，避免列挤压 */
  .el-table {
    overflow-x: auto;

    :deep(.el-table__body-wrapper) {
      width: 100%;
      min-width: 600px; /* 确保表格最小宽度，避免内容折叠 */
    }
  }

  /* 弹窗：小屏幕占满宽度 */
  :deep(.el-dialog) {
    width: 90% !important;
    margin: 0 auto !important;
  }
}
</style>